define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'house/index' + location.search,
                    add_url: 'house/add',
                    edit_url: 'house/edit',
                    del_url: 'house/del',
                    multi_url: 'house/multi',
                    import_url: 'house/import',
                    table: 'house',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'room_id', title: __('Room_id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'community', title: __('Community')},
                        {field: 'street', title: __('Street')},
                        {field: 'rent', title: __('Rent')},
                        {field: 'area', title: __('Area'), operate:'BETWEEN'},
                        {field: 'house_type', title: __('House_type'), operate: 'LIKE'},
                        {field: 'floor', title: __('Floor')},
                        {field: 'payment_type', title: __('Payment_type'), operate: 'LIKE'},
                        {field: 'face', title: __('Face'), operate: 'LIKE'},
                        {field: 'check_in', title: __('Check_in'), operate: 'LIKE'},
                        {field: 'rent_type', title: __('Rent_type')},
                        {field: 'decoration', title: __('Decoration')},
                        {field: 'deposit', title: __('Deposit'), operate:'BETWEEN'},
                        {field: 'water_rate', title: __('Water_rate'), operate:'BETWEEN'},
                        {field: 'power_rate', title: __('Power_rate'), operate:'BETWEEN'},
                        {field: 'manage_rate', title: __('Manage_rate'), operate:'BETWEEN'},
                        {field: 'facilities', title: __('Facilities'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'region', title: __('Region'), operate: 'LIKE'},
                        {field: 'notice', title: __('Notice'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'is_ax', title: __('Is_ax')},
                        {field: 'is_recommend', title: __('Is_recommend')},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'visit_num', title: __('Visit_num')},
                        {field: 'comment_score', title: __('Comment_score')},
                        {field: 'video', title: __('Video'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'cover', title: __('Cover'), operate: 'LIKE'},
                        {field: 'is_finish', title: __('Is_finish')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'kf_id', title: __('Kf_id')},
                        {field: 'intro', title: __('Intro'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'lng', title: __('Lng'), operate:'BETWEEN'},
                        {field: 'lat', title: __('Lat'), operate:'BETWEEN'},
                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    title: '复制房源',
                                    classname: 'btn btn-xs btn-success btn-copy btn-ajax',
                                    icon: 'fa fa-copy',
                                    confirm: '确认复制该房源？',
                                    url: 'house/copy',
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        $("#table").bootstrapTable('refresh',{silent:true});
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#c-street').data('params',function () {
                return {custom:{cid:3,pid:$('#c-community').val()}};
            });
            $(document).on('change','#c-community',function () {
                $('#c-street').selectPageClear();
            });
            Controller.api.bindevent();
        },
        edit: function () {

            $('#c-street').data('params',function () {
                return {custom:{cid:3,pid:$('#c-community').val()}};
            });
            $(document).on('change','#c-community',function () {
                $('#c-street').selectPageClear();
            });
            Controller.api.bindevent();
        },
        // copy: function(){
        //     Controller.api.bindevent();
        // },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
